import {message} from "antd";
import MessageQuery from "../components/message/MessageQuery";
import {useEffect, useState} from "react";
import HttpRequestUtils from "../utils/HttpRequestUtils";
import gengzpMqApis from "../apis/apis";
import MessageList from "../components/message/MessageList";

/**
 * 消息管理页面
 */
const Message = () => {

    const [messageApi, contextHolder] = message.useMessage();

    // 分页查询message条件
    const [pageQueryMessageParam, setPageQueryMessageParam] = useState({
        pageNum: 1,
        pageSize: 10,
        topic: "",
        messageKey: "",
        messageContent: "",
        status: null,
        statusValue: ""
    })

    // 分页查询message结果
    const [pageQueryMessageInfo, setPageQueryMessageInfo] = useState({
        messageList: [],
        total: 0,
        pageCount: 0
    })

    /**
     * 默认分页查询一次
     */
    useEffect(() => {
        pageQueryMessage();
    }, []);

    /**
     * 清空分页查询条件
     */
    const clearPageQueryParam = () => {
        setPageQueryMessageParam({
            pageNum: 1,
            pageSize: 10,
            topic: "",
            messageKey: "",
            messageContent: "",
            status: null,
            statusValue: ""
        });
    }

    /**
     * 分页查询消息
     */
    const pageQueryMessage = () => {
        HttpRequestUtils.post(gengzpMqApis.pageQueryMessage, {
            pageNum: pageQueryMessageParam.pageNum,
            pageSize: pageQueryMessageParam.pageSize,
            topic: pageQueryMessageParam.topic,
            messageKey: pageQueryMessageParam.messageKey,
            messageContent: pageQueryMessageParam.messageContent,
            status: pageQueryMessageParam.status
        }).then(res => {
            if (res.code === 200) {
                setPageQueryMessageInfo({
                    messageList: res.data.records,
                    total: res.data.total,
                    pageCount: res.data.pageCount
                })
            } else {
                messageApi.error(res.data.message);
            }
        })
    }

    /**
     * 分页查询死信队列消息
     */
    const pageQueryDeathMessage = () => {
        HttpRequestUtils.post(gengzpMqApis.pageQueryDeathMessage, {
            pageNum: pageQueryMessageParam.pageNum,
            pageSize: pageQueryMessageParam.pageSize,
            topic: pageQueryMessageParam.topic,
            messageKey: pageQueryMessageParam.messageKey,
            messageContent: pageQueryMessageParam.messageContent,
            status: pageQueryMessageParam.status
        }).then(res => {
            if (res.code === 200) {
                setPageQueryMessageInfo({
                    messageList: res.data.records,
                    total: res.data.total,
                    pageCount: res.data.pageCount
                })
            } else {
                messageApi.error(res.data.message);
            }
        })
    }

    return <div>
        {contextHolder}

        {/* 分页查询条件部分 */}
        <MessageQuery
            messageApi={messageApi}
            pageQueryMessageParam={pageQueryMessageParam}
            setPageQueryMessageParam={setPageQueryMessageParam}
            clearPageQueryParam={clearPageQueryParam}
            pageQueryMessageInfo={pageQueryMessageInfo}
            pageQueryMessage={pageQueryMessage}
            pageQueryDeathMessage={pageQueryDeathMessage}
        />

        {/* 消息列表展示部分 */}
        <MessageList
            messageApi={messageApi}
            pageQueryMessageInfo={pageQueryMessageInfo}
        />

    </div>
}

export default Message;